<?php
/**
 * This file is part of DomSocketServer.
 * 
 * DomSocketServer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomSocketServer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomSocketServer.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */

/*
 * 
 */
class DomSocketServerExtendedClient extends DomSocketServerClient {
	
	
	protected function onBind() {
		$this->server->sendToAll('Client['.$this->id.'] joined!');
		$this->send('Welcome. Youre id is '.$this->id);
	}
	
	protected function onClose() {
		$this->send('Goodbye. Server is shutting down!');
	}
	
	protected function onClosed() {
		
	}
	
	public function onData( $input ) {
		
		if( $input == '<policy-file-request/>' ) {

			$replay .= '<?xml version="1.0" encoding="UTF-8"?>';
			$replay .= '<cross-domain-policy>';
			$replay .= '<allow-access-from domain="*" to-ports="*" secure="false" />';
			$replay .= '</cross-domain-policy>';
			
			$this->send( $replay );
			$this->close();
		
		} else if( substr($input,0,3) === 'GET' ) {	

			$header  = "HTTP/1.1 200 OK\n";
			$header .= "Server: PHP Socket server\n";
			$header .= "Date: ".date("d, j M Y G:i:s T")."\n";
			$header .= "Last-Modified: ".date("d, j M Y G:i:s T")."\n";
			$header .= "Keep-Alive: timeout=5, max=100\n";
			$header .= "Connection: Keep-Alive\n";
			$header .= "Content-type: text/html\n";
			$header .= "\n";
			
			$header .= 'This server is not meant for HTTP requests!';
		
			$this->send( $header, false );
			$this->close();
			
	   	} else if( substr($input,0,3) == 'all' ) {
			$this->server->sendToAll( 'Client['.$this->id.']: '.substr($input,4) );
			
		} else if( $input == 'hello' ) {
			$this->send( $connection, "Hello you to!");
		
		} else {
			$this->send( $connection,'unknown command');
		}
		return true;
	}
}
?>